﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 *
 * Notice: this is only a "simple" program to demostrate how to use Google Geo Kit. It is not perfomance turned nor code refined
 * for any furthur question, please mail to jebberwocky@gmail.com, glad to answer your question
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//mongo db
using MongoDB;
using MongoDB.Linq;
using CtpGgglMapWinUI.Cache;
using System.Drawing;
using System.IO;

namespace Citiport.Demo.Cache
{
    public class MongoDBCache : IMapImageCache
    {
        public class CacheObj
        {
            public String Key { get; set; }
            public String Value { get; set; } 
        }

        private static IMongoDatabase connectDB()
        {
            var mongo = new Mongo();
            mongo.Connect();
            IMongoDatabase db = mongo.GetDatabase("gglsgmapscache");
            return db;
        }

        public static IMongoCollection<CacheObj> getCollections()
        {
            return connectDB().GetCollection<CacheObj>();
        }
        
        public void SetImage(System.Drawing.Image image, string key)
        {
            IMongoCollection<CacheObj> cols = getCollections();
            var v = cols.Linq<CacheObj>().FirstOrDefault<CacheObj>(x => x.Key == key);
            String base64String = String.Empty;
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imageBytes = ms.ToArray();
                base64String = Convert.ToBase64String(imageBytes);
            }
            if (v == null)
            {
                v = new CacheObj() { Key = key, Value = base64String };
                cols.Insert(v);
            }
            else
            {
                v.Value = base64String;
                cols.Save(v);
            }
        }

        public System.Drawing.Image GetImage(string key)
        {
            IMongoCollection<CacheObj> cols = getCollections();
            if (cols != null)
            {
                if (cols.Count() > 0)
                {
                    var v = cols.Linq<CacheObj>().FirstOrDefault<CacheObj>(x => x.Key == key);
                    if (v != null)
                    {
                        byte[] imageBytes = Convert.FromBase64String(v.Value);
                        MemoryStream ms = new MemoryStream(imageBytes, 0,
                          imageBytes.Length);
                        ms.Write(imageBytes, 0, imageBytes.Length);
                        Image image = Image.FromStream(ms, true);
                        return image;
                    }
                }
            }
            return null;
        }

        public void Clean()
        {
            var collection = connectDB().GetCollection<CacheObj>();
            collection.Remove(p => true);
        }
    }
}
